//
//  UIImage+FGChat.m
//  FGChatKit
//
//  Created by Henry on 2021/8/27.
//

#import "UIImage+FGChat.h"
#import <SDWebImage/SDWebImage.h>
#import "FGChatUserModel.h"
#import "FGResources.h"
#import "FGChatHelper.h"
#import "UIImage+FGUtil.h"
#import "UIImage+Addtions.h"
#import "FGChatConfiguration.h"
#import "NSString+FGHash.h"
#import "FGPathTool.h"

@implementation UIImage (FGChat)

+ (UIImage *)fg_setImage:(FGChatUserModel *)model {
    if (model.portraitImage) {
        return model.portraitImage;
    }
    UIImage *defaultHead = [FGResources getImageFromMessage:@"default_head"];
    if (model.userId.length == 0) {
        model.portraitImage = defaultHead;
        return defaultHead;
    }
    NSString *avatarName = [model.userId fg_md5String];
    NSString *imagePath = [FGChatHelper pathAvatarImage:avatarName];
    if ([FGPathTool isExistFile:imagePath]) {
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        model.portraitImage = image;
        return image;
    }
    
    if (model.portrait.length == 0) {
        // 已名称生成
        UIImage *image = [UIImage getImage:model.name];
        NSData *imgData = UIImageJPEGRepresentation(image, 1);
        [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imgData attributes:nil];
        model.portraitImage = image;
        return image;
    }
    
    // url 需要下载
    NSURL *url = [NSURL URLWithString:model.portrait];

    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url options:SDWebImageDecodeFirstFrameOnly|SDWebImageAllowInvalidSSLCertificates progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        
    } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
        
        if (error) {
            image = [UIImage getImage:model.name];
        }
        
        NSData *imgData = UIImageJPEGRepresentation(image, 1);
        [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imgData attributes:nil];
        model.portraitImage = image;
        
    }];
    
    return nil;
}

+ (UIImage *)fg_getConversationImage:(FGConversationModel *)model {
    NSMutableArray *imageArr = [NSMutableArray array];
    if (model.members.count > 0) {
        // TODO: 如何处理异步URL
        if (model.members.count == 2) {
            for (FGChatUserModel *user in model.members) {
                if (![FGChatConfiguration isSelf:user.userId]) {
                    [user configDefaultImage];
                    [imageArr addObject:user.portraitImage];
                }
            }
        } else {
            for (FGChatUserModel *user in model.members) {
                [user configDefaultImage];
                [imageArr addObject:user.portraitImage];
            }
        }
    } else {
        NSArray *names = [model.name componentsSeparatedByString:@","];
        for (NSString *name in names) {
            UIImage *image = [UIImage getImage:name];
            [imageArr addObject:image];
        }
    }
    
    UIImage *gImage = [UIImage groupIconWith:imageArr bgColor:[UIColor lightGrayColor]];
    NSString *name = [model.identifier fg_md5String];
    NSString *path = [FGChatHelper pathConvHead:model.identifier name:name];
    [[NSFileManager defaultManager] createFileAtPath:path contents:UIImageJPEGRepresentation(gImage, 1) attributes:nil];
    return gImage;
}

@end
